package com.xt.website_manager.dao;

import com.xt.website_manager.po.Category;
import com.xt.website_manager.po.CategoryExt;
import com.xt.website_manager.po.CategorySecond;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;
import java.util.List;

/*
 * @Author 爱尚实训赵国鑫
 * @ClassName  CategoryDao
 * @Date 2019/8/18/9:09
 * @Description TODO:(一句话描述这个类)
 */
@Mapper
public interface CategoryDao {
    /**
     * 获取一二级类目集合
     *
     * @return List<CategoryExt>
     */
    @Select("select * from tb_category order by sort asc")
    @Results(id = "categoryMap", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "cname", property = "cname"),
            @Result(column = "image", property = "image"),
            @Result(column = "publicityOne", property = "publicityOne"),
            @Result(column = "publicityTwo", property = "publicityTwo"),
            @Result(column = "sort", property = "sort"),
            @Result(column = "type", property = "type"),
            @Result(column = "id", property = "list",
                    many = @Many(select = "com.xt.website_manager.dao.CategoryDao.getCategorySecondsByCid"))
    })
    ArrayList<CategoryExt<CategorySecond>> getAllCategories();


    /**
     * 根据一级类目cid查询二级类目集合
     *
     * @param cid
     * @return
     */
    @Select("select * from tb_categorysecond where cid=#{cid} order by sort asc")
    ArrayList<CategorySecond> getCategorySecondsByCid(Integer cid);

    /**
     * 查询一级类目详情，并查询该类目下的全部二级类目
     */
    @Select("select * from tb_category where id = #{cid}")
    @ResultMap("categoryMap")
    CategoryExt<CategorySecond> findCategoryByCid(Integer cid);

    /**
     * 查询一级类目下的二级类目数目
     */
    @Select("select count(0) from tb_categorysecond where cid = #{cid}")
    Integer findCategoryCountByCid(Integer cid);

    /**
     * 根据csid查询二级类目信息
     */
    @Select("select * from tb_categorysecond where id = #{csid}")
    CategorySecond findCategorySecondByCsid(Integer csid);

    /**
     * 获取一级类目集合
     */
    @Select("select * from tb_category order by sort asc")
    List<Category> getCategories();

    /**
     * 查询全部二级类目
     */
    @Select("select * from tb_categorysecond")
    List<CategorySecond> getAllCategorysecond();


    /**
     * 根据类目类型查询二级类目
     */
    @Select("select * from tb_categorysecond where type = #{type}")
    List<CategorySecond> findCategorySecondByType(Integer type);


    /**
     * 根据二级类目查询其对应的一级类目
     */
    @Select("select * from tb_category where id =(\n" +
            "select cid from tb_categorysecond\n" +
            "where id = #{csid})")
    Category findCategoryByCsid(Integer csid);

    /**
     * 根据一级类目id一级查询类目
     *
     * @param cid
     * @return
     */
    @Select("select * from tb_category where id = #{cid}")
    Category getCategoryByCid(Integer cid);

    /**
     * 修改一级类目
     *
     * @param category
     */
    @Update("update tb_category set cname=#{cname},image=#{image},publicityOne=#{publicityOne},publicityTwo=#{publicityTwo},sort=#{sort},type=#{type} where id=#{id}")
    void updateCategory(Category category);

    /**
     * 添加一级类目
     *
     * @param category
     */
    @Insert("insert into tb_category values(default,#{cname},#{image},#{publicityOne},#{publicityTwo},#{sort},#{type})")
    void addCategory(Category category);

    /**
     * 删除一级类目
     *
     * @param id
     */
    @Delete("delete from tb_category where id=#{id}")
    void delCategory(Integer id);


    /**
     * 更改一级类目优先级
     *
     * @param category
     */
    @Update("update tb_category set sort=#{sort} where id=#{id}")
    void changeCategorySort(Category category);


    /**
     * 添加二级类目
     *
     * @param categorySecond
     */
    @Insert("insert into tb_categorysecond values(default,#{csname},#{image},#{publicityOne},#{publicityTwo},#{cid},#{sort},#{type})")
    void addCategorySecond(CategorySecond categorySecond);

    /**
     * 删除二级类目
     *
     * @param csid
     */
    @Delete("delete from tb_categorysecond where id=#{csid}")
    void delCategorySecond(Integer csid);

    /**
     * 更改二级类目优先级
     */
    @Update("update tb_categorysecond set sort=#{sort} where id=#{id}")
    void changeCategoryCsSort(CategorySecond categorySecond);

    /**
     * 修改二级类目
     * @param categorySecond
     */
    @Update("update tb_categorysecond set csname=#{csname},image=#{image},publicityOne=#{publicityOne},publicityTwo=#{publicityTwo},sort=#{sort},type=#{type},cid=#{cid} where id=#{id}")
    void updateCategorySecond(CategorySecond categorySecond);
}